# -*- coding: utf-8 -*-
# @Time    : 2025/10/13
# @Author  : Qwen
# @File    : MapCostService.py
# @Describe: 地图费用计算服务

import logging
from typing import Dict, Any
from luck.core.LuckConstant import LuckConstant

class MapCostService:
    def __init__(self):
        pass
    
    def calculate_cost(self, distance: float, unitPrice: float) -> Dict[str, Any]:
        """
        计算费用
        
        Args:
            distance: 里程数 (公里)
            unitPrice: 单价 (元/公里)
            
        Returns:
            dict: 费用计算结果
        """
        try:
            # 参数验证
            if distance < 0:
                return {
                    "code": LuckConstant.CODE_FAIL,
                    "msg": "里程数不能为负数"
                }
                
            if unitPrice < 0:
                return {
                    "code": LuckConstant.CODE_FAIL,
                    "msg": "单价不能为负数"
                }
            
            # 计算费用
            cost = distance * unitPrice
            
            # 返回结果
            return {
                "code": LuckConstant.CODE_SUCCESS,
                "msg": LuckConstant.MSG_SUCCESS,
                "data": {
                    "distance": distance,
                    "unitPrice": unitPrice,     
                    "cost": round(cost, 2)  # 保留两位小数
                }
            }
                
        except Exception as e:
            error_msg = f"费用计算错误: {str(e)}"
            logging.error(error_msg)
            return {
                "code": LuckConstant.CODE_FAIL,
                "msg": error_msg
            }